package com.hmall.order.task;

import com.hmall.feign.client.ItemClient;
import com.hmall.order.mapper.OrderMapper;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDateTime;
import java.util.List;

/**
 * TODO
 *
 * @Author lyh
 * @Date 2024/11/15 12:00
 */
//@Component
public class OutTimePay {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ItemClient itemClient;
    @Scheduled(cron = "0 0/1 * * * ?")
    public void outTimePay(){
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime time = now.plusMinutes(-30);
        List<OrderDetail> list = orderMapper.selectOutTimePay(time);
        if(list!=null&&list.size()>0){
            list.forEach(orderDetail -> {
                itemClient.recoverStock(Long.valueOf(orderDetail.getItemId()),orderDetail.getNum());
                Order order = orderMapper.selectById(orderDetail.getOrderId());
                order.setStatus(5);
                orderMapper.updateById(order);
            });
        }
    }
}
